package com.ethjava;

import com.ethjava.utils.Environment;
import org.web3j.crypto.*;
import org.web3j.protocol.admin.Admin;
import org.web3j.protocol.admin.methods.response.NewAccountIdentifier;
import org.web3j.protocol.admin.methods.response.PersonalListAccounts;
import org.web3j.protocol.admin.methods.response.PersonalUnlockAccount;
import org.web3j.protocol.http.HttpService;

import java.io.IOException;
import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.List;

/**
 * 账号管理相关
 */
public class AccountManager {
	private static Admin admin;

	public static void main(String[] args) {
		admin = Admin.build(new HttpService(Environment.RPC_URL));
//		createAccount();
//		createNewAccount();
//		getAccountList();
		unlockAccount();

//		admin.personalSendTransaction(); 该方法与web3j.sendTransaction 相同 不在此写例子。
	}

	public static void createAccount() {
		try {
			ECKeyPair ecKeyPair = Keys.createEcKeyPair();
			BigInteger privateKeyInDec = ecKeyPair.getPrivateKey();
			String privateKey = privateKeyInDec.toString(16);
			WalletFile aWallet = Wallet.createLight("123456789", ecKeyPair);
			String address = aWallet.getAddress();
			if (address.startsWith("0x")) {
				address = address.substring(2).toLowerCase();
			} else {
				address = address.toLowerCase();
			}
			address = "0x" + address;
			System.out.println("地址：" + address);
			System.out.println("秘钥：" + privateKey);
		} catch (InvalidAlgorithmParameterException |
				CipherException | NoSuchProviderException | NoSuchAlgorithmException e) {
			System.out.println(e.getCause().toString());
		}
	}

	/**
	 * 创建账号
	 */
	private static void createNewAccount() {
		String password = "123456789";
		try {
			NewAccountIdentifier newAccountIdentifier = admin.personalNewAccount(password).send();
			String address = newAccountIdentifier.getAccountId();
			System.out.println("new account address " + address);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取账号列表
	 */
	private static void getAccountList() {
		try {
			PersonalListAccounts personalListAccounts = admin.personalListAccounts().send();
			List<String> addressList;
			addressList = personalListAccounts.getAccountIds();
			System.out.println("account size " + addressList.size());
			for (String address : addressList) {
				System.out.println(address);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 账号解锁
	 */
	private static void unlockAccount() {
		String address = "0x024c3adeD3ACAD90687a8eD1B1460Ad8DE871534";
		String password = "123456789";
		//账号解锁持续时间 单位秒 缺省值300秒
		BigInteger unlockDuration = BigInteger.valueOf(60L);
		try {
			PersonalUnlockAccount personalUnlockAccount = admin.personalUnlockAccount(address, password, unlockDuration).send();
			Boolean isUnlocked = personalUnlockAccount.accountUnlocked();
			System.out.println("account unlock " + isUnlocked);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
